<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/5/14
 * Time: 10:47
 */

namespace app\model\pay;

use app\model\BaseModel;

class AppleSubscribeLogModel extends BaseModel
{
    protected $table = 'video_apple_subscribe_log';

    protected $primaryKey = 'id';

    public $timestamps = false;

    protected static $instance;

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function insertData($params)
    {
        if(!empty($params[0])){
            foreach($params as $param){
                $find = false;
                !empty($param['transaction_id']) && $find = $this->where(['transaction_id'=>$param['transaction_id']])->first();
                if($find==false){
                    $insert = [
                        'product_id' => $param['product_id']??'',
                        'transaction_id' => $param['transaction_id']??'',
                        'original_transaction_id' => $param['original_transaction_id']??'',
                        'purchase_date' => $param['purchase_date']??'',
                        'purchase_date_ms' => $param['purchase_date_ms']??'',
                        'purchase_date_pst' => $param['purchase_date_pst']??'',
                        'original_purchase_date' => $param['original_purchase_date']??'',
                        'original_purchase_date_ms' => $param['original_purchase_date_ms']??'',
                        'original_purchase_date_pst' => $param['original_purchase_date_pst']??'',
                        'expires_date' => $param['expires_date']??'',
                        'expires_date_ms' => $param['expires_date_ms']??'',
                        'expires_date_pst' => $param['expires_date_pst']??'',
                        'web_order_line_item_id' => $param['web_order_line_item_id']??'',
                        'is_trial_period' => $param['is_trial_period']??'',
                        'is_in_intro_offer_period' => $param['is_in_intro_offer_period']??'',
                        'in_app_ownership_type' => $param['in_app_ownership_type']??'',

                    ];

                    $this->insertGetId($insert);
                }
            }
            return true;
        }else{
            $param = $params;
            $find = false;
            !empty($param['transaction_id']) && $find = $this->where(['transaction_id'=>$param['transaction_id']])->first();
            if($find==false){
                $insert = [
                    'product_id' => $param['product_id']??'',
                    'transaction_id' => $param['transaction_id']??'',
                    'original_transaction_id' => $param['original_transaction_id']??'',
                    'purchase_date' => $param['purchase_date']??'',
                    'purchase_date_ms' => $param['purchase_date_ms']??'',
                    'purchase_date_pst' => $param['purchase_date_pst']??'',
                    'original_purchase_date' => $param['original_purchase_date']??'',
                    'original_purchase_date_ms' => $param['original_purchase_date_ms']??'',
                    'original_purchase_date_pst' => $param['original_purchase_date_pst']??'',
                    'expires_date' => $param['expires_date']??'',
                    'expires_date_ms' => $param['expires_date_ms']??'',
                    'expires_date_pst' => $param['expires_date_pst']??'',
                    'web_order_line_item_id' => $param['web_order_line_item_id']??'',
                    'is_trial_period' => $param['is_trial_period']??'',
                    'is_in_intro_offer_period' => $param['is_in_intro_offer_period']??'',
                    'in_app_ownership_type' => $param['in_app_ownership_type']??'',

                ];

                return $this->insertGetId($insert);
            }

            return true;
        }
    }
}